<?php

namespace app\merchant\controller;

use app\common\model\Goods as GoodsModel;
use app\common\controller\Tools;
use think\Db;
use app\common\model\GoodsCategory as GoodsCategoryModel;
use app\common\tool\OSSUtils;

class Goods extends Common
{
    public function goodsDetail()
    {
        $detailsId = (int)input('param.detailsId');
        $data = GoodsModel::getGoodsDetails($detailsId);
        return returnJson(1, 'Request is ok', $data);
    }

    public function upload_sku()
    {
        // 店铺编号+商户编号+时间戳+随机4位数
        $whereName = 'goods/sku';
        $data = Tools::upload_goods_img($whereName);
        return returnJson(1, '上传成功', $data);;
    }

    public function upload_rentlist()
    {
        // 店铺编号+商户编号+时间戳+随机4位数
        $whereName = 'goods/rentlist';
        $data = Tools::upload_goods_img($whereName);
        return $data;
    }

    public function upload_main_img()
    {
        // 店铺编号+商户编号+时间戳+随机4位数
        $whereName = 'goods/main';
        $data = Tools::upload_goods_img($whereName);
        return $data;
    }


    // 选择商品分类,插入数据
    public function insert()
    {
        $cat_id = (int)input('param.catId');
        $model = new GoodsModel;
        $model->goods_category_id = $cat_id;
        $model->goods_merchant_id = session('merchant.merchant_id');
        $result = $model->save();
        if (!$result) {
            return returnJson(0, '添加失败，请稍后重试');
        }
        $data['id'] = Db::name('goods')->getLastInsID();
        $data['catId'] = $cat_id;
        return returnJson(1, '操作成功', $data);
    }


    public function updateCatName()
    {
        $catId = (int)input('param.kartik-input-711[]');
        $goodsId = (int)input('param.goods_id');
        $result = GoodsModel::where('goods_id', $goodsId)->setField('goods_category_id', $catId);
        if ($result) {
            $data['id'] = $goodsId;
            $data['catId'] = $catId;
            return returnJson(1, 'Select category is ok', $data);
        } else {
            return returnJson(1, '');
        }
    }

    // 删除商品
    public function delete()
    {
        $goods_id = (int)input('param.goods_id');
        $result = GoodsModel::changeDel($goods_id);

        if (!$result)
            return reutrnJson(0, '删除成功');
        return returnJson(1, '删除成功');
    }

    // 商品新增
    public function addgoods()
    {
        // 租赁规则模板
        $lease_tpl = GoodsModel::getLeaseTpl();
        // 商品详情模板
        $detail_tpl = GoodsModel::getDetailTpl();

        $freight_tpl_type = GoodsModel::getFreigtTpl();

        // 快递模板
        $express_tpl = $freight_tpl_type['express_tpl'];

        // 上门模板
        $to_door = $freight_tpl_type['to_door'];

        // 自提点
        $sinces = GoodsModel::getSinces();

        // 归还地址
        $return_place = GoodsModel::getReturnPlace();

        // 加载商家商品通用属性默认值
        $common_attrs = GoodsModel::getCommonAttr();

        //分类
        $datacate = GoodsCategoryModel::getParentCategory();
        $this->assign([
            'lease_tpl' => $lease_tpl,
            'detail_tpl' => $detail_tpl,
            'sinces' => $sinces,
            'express_tpl' => $express_tpl,
            'to_door' => $to_door,
            'return_place' => $return_place,
            'datacate' => $datacate,
            'common_attrs' => $common_attrs
        ]);
        return view('update');
    }

    public function savegoods($shelf_state)
    {

        $data['goods_merchant_id'] = input('param.goods_merchant_id');
        $data['goods_category_id'] = input('param.goods_category_id');
        $data['goods_name'] = input('param.goods_name');
        $data['goods_is_free_deposit'] = input('param.goods_is_free_deposit') == '1' ? 1 : 2;
        //$data['goods_is_free_shipping'] = input('param.goods_is_free_shipping') == '1' ? 1 : 2;
        $data['goods_is_deductible'] = input('param.goods_is_deductible') == '1' ? 1 : 2;
        //$data['goods_is_door'] = input('param.goods_is_door') == '1' ? 1 : 2;
        $data['goods_is_follow_lease'] = input('param.goods_is_follow_lease') == '1' ? 1 : 2;
        //$data['goods_is_since'] = input('param.goods_is_since') == '1' ? 1 : 2;
        $data['goods_leasing_rules'] = input('param.goods_leasing_rules', 0);
        $data['goods_goods_detail_id'] = input('param.goods_goods_detail_id', 0);
        $data['goods_revert_id'] = input('param.goods_revert_id/a');
        $data['goods_shelf_state'] = $shelf_state;
         if(input('param.goods_max_count')!='')
         {
           $data['goods_max_count']=input('param.goods_max_count');
         }
        if ($shelf_state == 2) {
            $data['goods_shelf_time'] = time();
        }

        $data['goods_create_time'] = time();
       
        if ($data['goods_revert_id']) {
            $data['goods_revert_id'] = implode(',', $data['goods_revert_id']);
        }
        
        $data['goods_is_verify_real_name'] = input('param.goods_is_verify_real_name');

        //图片格式化
        /*        $arr=[];
                $goods_details_images = input('param.file_details');
                $details_images = explode("|", $goods_details_images);
                $new_details_images = array_values(array_filter($details_images));

                //
                $arrs = [];
                $count = count($new_details_images);
                for ($i = 0; $i < $count; $i++) {
                    $arr['image'] = $new_details_images[$i];
                    $arr['text'] = '';//$goods_details_name[$i];
                    $arrs[$i] = $arr;
                }
                $data['goods_details_content'] = json_encode($arrs, JSON_UNESCAPED_UNICODE);*/
        //商品详情图片
        $detailImgs = input('goods_details_images/a');
        $detailImgs = explode(',', $detailImgs[0]);
        $detailImgArr = [];
        foreach ($detailImgs as $detailImg) {
            $detailImgArr[] = ['image' => $detailImg, 'text' => ''];
        }
        $data['goods_details_content'] = json_encode($detailImgArr, JSON_UNESCAPED_UNICODE);
        $goods_store_id = GoodsModel::getStoreId();
        $data['goods_store_id'] = $goods_store_id;
        GoodsModel::insertData($data);
    }


    // 商品保存修改
    public function update()
    {
        $this->savegoods(1);
        return returnJson(1, "新增成功!");
    }


    // 商品上架
    public function update2()
    {
        $this->savegoods(2);
        return returnJson(1, "上架成功!");
    }

    public function add()
    {
        return view('add');
    }

    public function showlist()
    {
        $pagesize = (int)input('param.DataTables_Table_0_length', 10);
        $type = input('param.type') ? input('param.type') : 0;
        $data = GoodsModel::showlist($pagesize, $type);
        $this->assign([
            'data' => $data,
            'type' => $type,
            'pagesizes' => $pagesize,
        ]);

        return view('showlist');
    }

    /**
     *
    */
    public function chanelRentInfo($color=array(),$guige=array(),$sameAttr=array(),$rentPeriod=array()){
        $rent_type_list = [];//最终数据预定义
        $rchild = [];//租用周期信息预定义
        $gchild = [];//商品公有信息预定义
        $rtype = '';
        if(!empty($color) && !empty($guige)){
            foreach($color as $key => $value) {
                foreach ($guige as $gkey => $gvalue) {
                    foreach ($sameAttr as $skey => $svalue) {
                        if (strpos($svalue['content_var_attr_id'], $gvalue['content_id'] . "") !== false && strpos($svalue['content_var_attr_id'], $value['content_id'] . "") !== false) {
                            //先定位处理颜色规格下的租用信息
                            foreach ($rentPeriod['data'] as $rkey => $rvalue) {
                                if ($rvalue['rent_period_goods_attr_id'] == $svalue['content_id']) {
                                    if ($rvalue['rent_period_type'] == 1) {
                                        $rtype = '日';
                                    }
                                    if ($rvalue['rent_period_type'] == 2) {
                                        $rtype = '周';
                                    }
                                    if ($rvalue['rent_period_type'] == 3) {
                                        $rtype = '月';
                                    }
                                    if ($rvalue['rent_period_type'] == 4) {
                                        $rtype = '季';
                                    }
                                    if ($rvalue['rent_period_type'] == 5) {
                                        $rtype = '年';
                                    }
                                    $rchild[] = array(
                                        'time' => $rtype,
                                        'children' => array(
                                            'min_rent' => $rvalue['rent_period_min_rent'],
                                            'max_rent' => $rvalue['rent_period_max_rent'],
                                            'oldprice' => $rvalue['rent_period_old_rent_price'],
                                            'currentprice' => $rvalue['rent_period_now_rent_price'],
                                            'min_advance' => $rvalue['rent_period_min_advance'],
                                            'max_advance' => $rvalue['rent_period_max_advance']/30,
                                            'relet_discount' => $rvalue['relet_discount']
                                        )
                                    );
                                }
                            }
                            //生成租用信息和公有属性
                            $gchild[] = array(
                                'size' => empty($gvalue['value']) ? "" : $gvalue['value'],
                                'price' => $svalue['market_price'],
                                'deposit' => $svalue['goods_deposit'],
                                'children' => $rchild
                            );
                            $rchild = [];
                        }
                    }
                }
                //生成最终数据
                $rent_type_list[] = array(
                    'color' => empty($value['value']) ? "" : $value['value'],
                    'children' => $gchild,
                );
                $gchild = [];
            }
        }
        if(!empty($color) && empty($guige)){
            foreach($color as $key => $value) {
                    foreach ($sameAttr as $skey => $svalue) {
                        if (strpos($svalue['content_var_attr_id'], $value['content_id'] . "") !== false) {
                            //先定位处理颜色规格下的租用信息
                            foreach ($rentPeriod['data'] as $rkey => $rvalue) {
                                if ($rvalue['rent_period_goods_attr_id'] == $svalue['content_id']) {
                                    if ($rvalue['rent_period_type'] == 1) {
                                        $rtype = '日';
                                    }
                                    if ($rvalue['rent_period_type'] == 2) {
                                        $rtype = '周';
                                    }
                                    if ($rvalue['rent_period_type'] == 3) {
                                        $rtype = '月';
                                    }
                                    if ($rvalue['rent_period_type'] == 4) {
                                        $rtype = '季';
                                    }
                                    if ($rvalue['rent_period_type'] == 5) {
                                        $rtype = '年';
                                    }
                                    $rchild[] = array(
                                        'time' => $rtype,
                                        'children' => array(
                                            'min_rent' => $rvalue['rent_period_min_rent'],
                                            'max_rent' => $rvalue['rent_period_max_rent'],
                                            'oldprice' => $rvalue['rent_period_old_rent_price'],
                                            'currentprice' => $rvalue['rent_period_now_rent_price'],
                                            'min_advance' => $rvalue['rent_period_min_advance'],
                                            'max_advance' => $rvalue['rent_period_max_advance'] / 30,
                                            'relet_discount' => $rvalue['relet_discount']
                                        )
                                    );
                                }
                            }
                            //生成租用信息和公有属性
                            $gchild[] = array(
                                'size' => "",
                                'price' => $svalue['market_price'],
                                'deposit' => $svalue['goods_deposit'],
                                'children' => $rchild
                            );
                            $rchild = [];
                        }
                }
                //生成最终数据
                $rent_type_list[] = array(
                    'color' => empty($value['value']) ? "" : $value['value'],
                    'children' => $gchild,
                );
                    $gchild = [];
            }
        }
        if(empty($color) && !empty($guige)) {
            foreach ($guige as $gkey => $gvalue) {
                foreach ($sameAttr as $skey => $svalue) {
                    if (strpos($svalue['content_var_attr_id'], $gvalue['content_id'] . "") !== false ) {
                        //先定位处理颜色规格下的租用信息
                        foreach ($rentPeriod['data'] as $rkey => $rvalue) {
                            if ($rvalue['rent_period_goods_attr_id'] == $svalue['content_id']) {
                                if ($rvalue['rent_period_type'] == 1) {
                                    $rtype = '日';
                                }
                                if ($rvalue['rent_period_type'] == 2) {
                                    $rtype = '周';
                                }
                                if ($rvalue['rent_period_type'] == 3) {
                                    $rtype = '月';
                                }
                                if ($rvalue['rent_period_type'] == 4) {
                                    $rtype = '季';
                                }
                                if ($rvalue['rent_period_type'] == 5) {
                                    $rtype = '年';
                                }
                                $rchild[] = array(
                                    'time' => $rtype,
                                    'children' => array(
                                        'min_rent' => $rvalue['rent_period_min_rent'],
                                        'max_rent' => $rvalue['rent_period_max_rent'],
                                        'oldprice' => $rvalue['rent_period_old_rent_price'],
                                        'currentprice' => $rvalue['rent_period_now_rent_price'],
                                        'min_advance' => $rvalue['rent_period_min_advance'],
                                        'max_advance' => $rvalue['rent_period_max_advance']/30,
                                        'relet_discount' => $rvalue['relet_discount']
                                    )
                                );
                            }
                        }
                        //生成租用信息和公有属性
                        $gchild[] = array(
                            'size' => empty($gvalue['value']) ? "" : $gvalue['value'],
                            'price' => $svalue['market_price'],
                            'deposit' => $svalue['goods_deposit'],
                            'children' => $rchild
                        );
                        $rchild = [];
                    }
                }
            }
            //生成最终数据
            $rent_type_list[] = array(
                'color' => "",
                'children' => $gchild,
            );
        }
        if(empty($color)&&empty($guige)){
            foreach ($sameAttr as $skey => $svalue) {
                //先定位处理颜色规格下的租用信息
                foreach ($rentPeriod['data'] as $rkey => $rvalue) {
                    if ($rvalue['rent_period_goods_attr_id'] == $svalue['content_id']) {
                        if ($rvalue['rent_period_type'] == 1) {
                            $rtype = '日';
                        }
                        if ($rvalue['rent_period_type'] == 2) {
                            $rtype = '周';
                        }
                        if ($rvalue['rent_period_type'] == 3) {
                            $rtype = '月';
                        }
                        if ($rvalue['rent_period_type'] == 4) {
                            $rtype = '季';
                        }
                        if ($rvalue['rent_period_type'] == 5) {
                            $rtype = '年';
                        }
                        $rchild[] = array(
                            'time' => $rtype,
                            'children' => array(
                                'min_rent' => $rvalue['rent_period_min_rent'],
                                'max_rent' => $rvalue['rent_period_max_rent'],
                                'oldprice' => $rvalue['rent_period_old_rent_price'],
                                'currentprice' => $rvalue['rent_period_now_rent_price'],
                                'min_advance' => $rvalue['rent_period_min_advance'],
                                'max_advance' => $rvalue['rent_period_max_advance']/30,
                                'relet_discount' => $rvalue['relet_discount']
                            )
                        );
                    }
                }
                //生成租用信息和公有属性
                $gchild[] = array(
                    'size' => "",
                    'price' => $svalue['market_price'],
                    'deposit' => $svalue['goods_deposit'],
                    'children' => $rchild
                );
                $rchild = [];
            }

            //生成最终数据
            $rent_type_list[] = array(
                'color' => "",
                'children' => $gchild,
            );
        }
        return $rent_type_list;
    }

    public function goods_edit()
    {
        // 修改所需数据
        // 商品id
        $goods_id = (int)input('param.goods_id');
        // 商品可变属性
        $varAttr = GoodsModel::getVarAttr($goods_id);
        $color = $varAttr['color'];//商品颜色信息
        $guige = $varAttr['guige'];//商品规格信息
        // 商品共有属性
        $sameAttr = GoodsModel::getSameAttr($goods_id);
        // 商品租用周期及相关属性
        $rentPeriod = GoodsModel::getRentPeriod($goods_id);

        /**
         * 前端vue渲染租用信息数据约定格式
         *  typedata:[{
         *      color:"",
         *      children: [{
         *          size: "",// 规格
         *          price: null, // 市场价
         *          deposit:null,// 押金
         *          children:[{
         *              time:"日",
         *              children:{
         *                  min_rent: null,// 最小周期
         *                  max_rent: null,//  最大周期
         *                  oldprice: null,// 原租价
         *                  currentprice: null,// 现租价
         *                  min_advance: null,// 最少提前
         *                  max_advance: null,// 最多提前
         *                  relet_discount: 0,// 续租折扣
         *              }
         *          }]
         *      },]
         * }]
        */
        $rent_type_list = $this->chanelRentInfo($color,$guige,$sameAttr,$rentPeriod);

        $goodsdata = GoodsModel::find($goods_id);
        $goodssku = GoodsModel::getSku($goods_id);
        $goodsmainimg = GoodsModel::getMainImg($goods_id);
        //租赁清单
        $goodsrentlist = GoodsModel::getrentlist($goods_id);
        //归还地址
        $ghdz = explode(',', $goodsdata['goods_revert_id']);
        //分类
        $datacate = GoodsCategoryModel::getParentCategory();
        // 商品类目
        $catData = GoodsModel::getCategory($goodsdata['goods_category_id']);
        $goodmarketrate = GoodsModel::getGoodsMarketRate($goodsdata['goods_activity_id']);
        if(empty($goodmarketrate) || !($goodmarketrate['status'] == 1 && $goodmarketrate['start_time'] < time() && $goodmarketrate['end_time'] > time())){
            $goodmarketrate = 0;
        }else{
            $goodmarketrate = $goodmarketrate['market_rate'];
        }
        $this->assign([
            'color' => $color,
            'rent_type_list'=>$rent_type_list,
            'good_act_rate'=>$goodmarketrate,
            'guige' => $guige,
            'sameAttr' => $sameAttr,
            'rentPeriod' => $rentPeriod['data'],
            //'rentPeriodType' => $rentPeriod['rent_period_type'],
            'goods_id' => $goods_id,
            'goodsdata' => $goodsdata,
            'catId' => $catData['goods_category_id'],
            'catName' => $catData['goods_category_name'],
            'goodssku' => $goodssku,
            'goodsmainimg' => $goodsmainimg,
            'ghdz' => $ghdz,
            'datacate' => $datacate,
            'goodsrentlist' => $goodsrentlist,
        ]);

        $img_details = json_decode($goodsdata['goods_details_content'], true);
          
        $imgRootPath = config('oss_imgRootPath');
        $img_detailscount = count($img_details);
        for ($i = 0; $i < $img_detailscount; $i++) {
            $img_details[$i]['imgUrl'] = $imgRootPath . 'goods/details/' . $img_details[$i]['image'];
        }

        $rent_period_type = GoodsModel::getrent_period_type($goods_id);

        if (!$goods_id)
            returnJson(0, 'ID参数错误');
        // 商品信息
        $goodsdata = GoodsModel::find($goods_id);
        $data = GoodsModel::getGoodsId($goods_id);


        // 租赁规则模板
        $lease_tpl = GoodsModel::getLeaseTpl();
        // 商品详情模板
        $detail_tpl = GoodsModel::getDetailTpl();

        $freight_tpl_type = GoodsModel::getFreigtTpl();

        // 快递模板
        $express_tpl = $freight_tpl_type['express_tpl'];

        //所选快递模板项
        $kdmoban = GoodsModel::getmobant($goods_id, 1);
        $smmoban = GoodsModel::getmobant($goods_id, 2);
        $ztmoban = GoodsModel::getmobantname($goods_id, 3);

        //var_dump($ztmoban);exit;

        // 上门模板
        $to_door = $freight_tpl_type['to_door'];

        // 自提点
        $sinces = GoodsModel::getSinces();

        // 归还地址
        $return_place = GoodsModel::getReturnPlace();

        //var_dump($img_details);exit;

        //商品表
        $data = GoodsModel::getGoodsId($goods_id);

        $this->assign([
            'lease_tpl' => $lease_tpl,
            'detail_tpl' => $detail_tpl,
            'sinces' => $sinces,
            'express_tpl' => $express_tpl,
            'to_door' => $to_door,
            'return_place' => $return_place,
            '$goodsdata' => $goodsdata,
            'rent_period_type' => $rent_period_type,
            //'arrayColor_Guige' => $arrays,
            //'arraylists' => $arraylists,
            'data' => $data,
            'kdmoban' => $kdmoban,
            'smmoban' => $smmoban,
            'ztmoban' => $ztmoban,
            'img_details' => $img_details,
        ]);

        // 修改开始
        if (request()->isAjax()) {
            return returnJson(1, "修改成功");
        }

        return view('updategoods');
    }

    function updategoods1()
    {
        $goods_store_id = GoodsModel::getStoreId();
        if (request()->isAjax()) {
            $data['goods_merchant_id'] = input('param.goods_merchant_id');
            $data['goods_category_id'] = input('param.goods_category_id');
            $data['goods_name'] = input('param.goods_name');
            $data['goods_is_free_deposit'] = input('param.goods_is_free_deposit') == 'on' ? 1 : 2;
            //$data['goods_is_free_shipping'] = input('param.goods_is_free_shipping') == 'on' ? 1 : 2;
            $data['goods_is_deductible'] = input('param.goods_is_deductible') == 'on' ? 1 : 2;
            //$data['goods_is_door'] = input('param.goods_is_door') == 'on' ? 1 : 2;
            $data['goods_is_follow_lease'] = input('param.goods_is_follow_lease') == 'on' ? 1 : 2;
            //$data['goods_is_since'] = input('param.goods_is_since') == 'on' ? 1 : 2;
            $data['goods_leasing_rules'] = input('param.goods_leasing_rules');
            $data['goods_goods_detail_id'] = input('param.goods_goods_detail_id');
            $data['goods_revert_id'] = input('param.goods_revert_id/a');
            $data['goods_max_count'] = input('param.goods_max_count');//new add
           
            $data['goods_shelf_state'] = '2';
            $data['goods_shelf_time'] = time();
            //$data['goods_create_time'] = time();
            $data['goods_update_time'] = time();
            //图片格式化
/*            $goods_details_images = input('param.goods_details_images/a');
            $goods_details_name = input('param.goods_details_name/a');
            $arrs = [];
            $count = count($goods_details_images);
            for ($i = 0; $i < $count; $i++) {
                $arr['image'] = $goods_details_images[$i];
                $arr['text'] = $goods_details_name[$i];
                $arrs[$i] = $arr;
            }
            $data['goods_details_content'] = json_encode($arrs, JSON_UNESCAPED_UNICODE);*/

            $detailImgs = input('goods_details_images/a');
            $detailImgs = explode(',', $detailImgs[0]);
            $detailImgArr = [];
            foreach ($detailImgs as $detailImg) {
                $detailImgArr[] = ['image' => $detailImg, 'text' => ''];
            }
            $data['goods_details_content'] = json_encode($detailImgArr, JSON_UNESCAPED_UNICODE);
            if ($data['goods_revert_id']) {
                $data['goods_revert_id'] = implode(',', $data['goods_revert_id']);
            }
            $data['goods_is_verify_real_name'] = input('param.goods_is_verify_real_name');
            $data['goods_leasing_rules'] = input('param.goods_leasing_rules');
            $data['goods_store_id'] = $goods_store_id;
            $data['goods_id'] = input('param.goods_id');
            //$data['method'] = 'update';
            GoodsModel::updateData($data);
            //GoodsModel::changeDel(input('param.goods_id'));
            return returnJson(1, "修改成功!");
        }
    }

    function updategoods2()
    {
        //店铺id

        $goods_store_id = GoodsModel::getStoreId();
        // 修改开始
        if (request()->isAjax()) {

            $data['goods_merchant_id'] = input('param.goods_merchant_id');
            $data['goods_category_id'] = input('param.goods_category_id');
            $data['goods_name'] = input('param.goods_name');
            $data['goods_is_free_deposit'] = input('param.goods_is_free_deposit') == 'on' ? 1 : 2;
            //$data['goods_is_free_shipping'] = input('param.goods_is_free_shipping') == 'on' ? 1 : 2;
            $data['goods_is_deductible'] = input('param.goods_is_deductible') == 'on' ? 1 : 2;
            //$data['goods_is_door'] = input('param.goods_is_door') == 'on' ? 1 : 2;
            $data['goods_is_follow_lease'] = input('param.goods_is_follow_lease') == 'on' ? 1 : 2;
           // $data['goods_is_since'] = input('param.goods_is_since') == 'on' ? 1 : 2;
            $data['goods_leasing_rules'] = input('param.goods_leasing_rules');
            $data['goods_goods_detail_id'] = input('param.goods_goods_detail_id');
            $data['goods_revert_id'] = input('param.goods_revert_id/a');
            $data['goods_max_count'] = input('param.goods_max_count');//new add
            $data['goods_shelf_state'] = '1';
            $data['goods_shelf_time'] = time();
            //$data['goods_create_time'] = time();
            $data['goods_update_time'] = time();
            
            //图片格式化
            $goods_details_images = input('param.goods_details_images/a');
            $goods_details_name = input('param.goods_details_name/a');
            $goods_details_images = explode(',', $goods_details_images[0]);

            $arrs = [];
            $count = count($goods_details_images);
            for ($i = 0; $i < $count; $i++) {
                $arr['image'] = $goods_details_images[$i];
                $arr['text'] = $goods_details_name[$i];
                $arrs[$i] = $arr;
            }
            $data['goods_details_content'] = json_encode($arrs, JSON_UNESCAPED_UNICODE);
            if ($data['goods_revert_id']) {
                $data['goods_revert_id'] = implode(',', $data['goods_revert_id']);
            }
            $data['goods_is_verify_real_name'] = input('param.goods_is_verify_real_name');
            $data['goods_leasing_rules'] = input('param.goods_leasing_rules');
            $data['goods_store_id'] = $goods_store_id;
            $data['goods_id'] = input('param.goods_id');

            $resultId = GoodsModel::updateData($data);
            //GoodsModel::changeDel(input('param.goods_id'));
            return returnJson(1, "修改成功!");


        }
    }

    function updategoods3()
    {
        //店铺id

        $goods_store_id = GoodsModel::getStoreId();
        // 修改开始
        if (request()->isAjax()) {

            $data['goods_merchant_id'] = input('param.goods_merchant_id');
            $data['goods_category_id'] = input('param.goods_category_id');
            $data['goods_name'] = input('param.goods_name');
            $data['goods_is_free_deposit'] = input('param.goods_is_free_deposit') == 'on' ? 1 : 2;
            //$data['goods_is_free_shipping'] = input('param.goods_is_free_shipping') == 'on' ? 1 : 2;
            $data['goods_is_deductible'] = input('param.goods_is_deductible') == 'on' ? 1 : 2;
            //$data['goods_is_door'] = input('param.goods_is_door') == 'on' ? 1 : 2;
            $data['goods_is_follow_lease'] = input('param.goods_is_follow_lease') == 'on' ? 1 : 2;
            //$data['goods_is_since'] = input('param.goods_is_since') == 'on' ? 1 : 2;
            $data['goods_leasing_rules'] = input('param.goods_leasing_rules');
            $data['goods_goods_detail_id'] = input('param.goods_goods_detail_id');
            $data['goods_revert_id'] = input('param.goods_revert_id/a');
            $data['goods_max_count'] = input('param.goods_max_count');//new add
            $data['goods_shelf_state'] = '3';
            $data['goods_shelf_time'] = time();
            //$data['goods_create_time'] = time();
            $data['goods_update_time'] = time();

            //图片格式化
            $goods_details_images = input('param.goods_details_images/a');
            $goods_details_name = input('param.goods_details_name/a');
            $goods_details_images = explode(',', $goods_details_images[0]);

            $arrs = [];
            $count = count($goods_details_images);
            for ($i = 0; $i < $count; $i++) {
                $arr['image'] = $goods_details_images[$i];
                $arr['text'] = $goods_details_name[$i];
                $arrs[$i] = $arr;
            }
            $data['goods_details_content'] = json_encode($arrs, JSON_UNESCAPED_UNICODE);
            if ($data['goods_revert_id']) {
                $data['goods_revert_id'] = implode(',', $data['goods_revert_id']);
            }
            $data['goods_is_verify_real_name'] = input('param.goods_is_verify_real_name');
            $data['goods_leasing_rules'] = input('param.goods_leasing_rules');
            $data['goods_store_id'] = $goods_store_id;
            $data['goods_id'] = input('param.goods_id');
            //$resultId = GoodsModel::insertData($data);
            $resultId = GoodsModel::updateData($data);
            //GoodsModel::changeDel(input('param.goods_id'));
            return returnJson(1, "修改成功!");


        }
    }


    //下架
    public function goods_putdown()
    {

        // 修改所需数据
        // 商品id
        $goods_id = (int)input('param.goods_id');
        $return_place = GoodsModel::putdown($goods_id);


        if (request()->isAjax()) {

            return returnJson(1, "下架成功");
        }


    }

    //上架
    public function goods_puton()
    {

        // 修改所需数据
        // 商品id
        $goods_id = (int)input('param.goods_id');
        $return_place = GoodsModel::puton($goods_id);
        $type = input('param.type') ? input('param.type') : 0;

        if (request()->isAjax()) {

            return returnJson(1, "上架成功");
        }

    }

    // 快递模板
    public function refresh_kd()
    {
        $freight_tpl_type = GoodsModel::getFreigtTpl();

        if ($freight_tpl_type['express_tpl']) {
            return returnJson(1, '刷新成功', $freight_tpl_type['express_tpl']);
        } else {
            return returnJson(1, '刷新失败，请稍后重试！');
        }


    }

    // 上门模板
    public function refresh_sm()
    {
        $freight_tpl_type = GoodsModel::getFreigtTpl();

        if ($freight_tpl_type['to_door']) {
            return returnJson(1, '刷新成功', $freight_tpl_type['to_door']);
        } else {
            return returnJson(1, '刷新失败，请稍后重试！');
        }


    }

    public function showpage()
    {
        return view('showpage');
    }

    //获取图片
    public function getGoodsDetails()
    {
        $imgRootPath = config('imgRootPath');
        $data = GoodsModel::getImgGoodsDetails($_POST['goods_details_id']);
        $returnData['url'] = $imgRootPath . '/goods/details/' . json_decode($data['goods_details_content'], true)[0]['image'];
        $returnData['img'] = json_decode($data['goods_details_content'], true)[0]['image'];
        $returnData['name'] = json_decode($data['goods_details_content'], true)[0]['text'];
        return returnJson(1, '成功', $returnData);
    }

    public function batch_upload_img($whereName)
    {
        $imgpath = config('img_path');
        $imgRootPath = config('imgRootPath');
        $storeId = GoodsModel::getStoreId();
        $merchantId = session('merchant.merchant_id');
        $old_filename = $storeId . $merchantId . time();

        $uploaddir = config('img_path') . $whereName . '/';
        $name = $_FILES['file']['name'];

        $type = strtolower(substr(strrchr($name, '.'), 1));
        //获取文件类型
        if (!is_dir($uploaddir)) {
            mkdir($uploaddir);
        }
        $typeArr = array("jpg", "png", "gif");
        if (!in_array($type, $typeArr)) {
            return returnJson(0, '请上传jpg,png或gif类型的图片！');
        }
        $filename = $storeId . $merchantId . time() . mt_rand(1000, 9999);
        move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $filename . '.' . $type);
        return $filename . '.' . $type;
    }

    /**
     * 上传图片
     */
    public function uploadImg2()
    {
        $type = input('type', 0);  //1-商品主图 2-商品详情 3-租赁清单 4-SKU
        if ($type == 1) {
            $imgPath = config('img_path') . 'goods/main';
            $imgRoot = config('imgRootPath') . 'goods/main/';
        } elseif ($type == 2) {
            $imgPath = config('img_path') . 'goods/details';
            $imgRoot = config('imgRootPath') . 'goods/details/';
        } elseif ($type == 3) {
            $imgPath = config('img_path') . 'goods/rentlist';
            $imgRoot = config('imgRootPath') . 'goods/rentlist/';
        } elseif ($type == 4) {
            $imgPath = config('img_path') . 'goods/sku';
            $imgRoot = config('imgRootPath') . 'goods/sku/';
        } else {
            return returnJson(0, '图片类型获取失败', '');
        }
        $targetDir = $imgPath;
        if (!is_dir($targetDir)) {
            @mkdir($targetDir, '', true);
           /*  chmod($targetDir, '0777'); */
        }
        $imgName = input('name');
        if (empty($imgName)) {
            return returnJson(0, '图片名称获取失败', '');
        }
        if (!move_uploaded_file($_FILES['file']['tmp_name'], $targetDir . '/' . $imgName)) {
            return returnJson(0, '上传失败', '');
        }
        $imgUrl = $imgName;
        return returnJson(1, '上传成功', ['imgUrl' => $imgUrl, 'imgPath' => $imgRoot]);
    }

    public function uploadImg()
    {
        $type = input('type', 0);  //1-商品主图 2-商品详情 3-租赁清单 4-SKU

        if ($type == 1) {
            $imgPath = config('oss_img_path') . 'goods/main';
            $imgRoot = config('oss_imgRootPath') . 'goods/main/';
        } elseif ($type == 2) {
            $imgPath = config('oss_img_path') . 'goods/details';
            $imgRoot = config('oss_imgRootPath') . 'goods/details/';
        } elseif ($type == 3) {
            $imgPath = config('oss_img_path') . 'goods/rentlist';
            $imgRoot = config('oss_imgRootPath') . 'goods/rentlist/';
        } elseif ($type == 4) {
            $imgPath = config('oss_img_path') . 'goods/sku';
            $imgRoot = config('oss_imgRootPath') . 'goods/sku/';
        } else {
            return returnJson(0, '图片类型获取失败', '');
        }
        $imgName = input('name');
        if (empty($imgName)) {
            return returnJson(0, '图片名称获取失败', '');
        }
        $imgUrl = $imgName;
        $oss = new OSSUtils();
        $res = $oss->uploadFile($imgPath."/".$imgName,$_FILES['file']['tmp_name']);
        $res = json_decode($res,true);
        if($res['status'] == 1){
            return returnJson(1, '上传成功', ['imgUrl' => $imgUrl, 'imgPath' => $imgRoot]);
        }else{
            return returnJson(0, $res['msg'], ['imgUrl' => $imgUrl, 'imgPath' => $imgRoot]);
        }
    }

}